const models = require('../db')
const express = require('express')
const router = express.Router()
const mysql = require('mysql')
const $sql = require('../sqlMap')
// 连接数据库
const conn = mysql.createConnection(models.mysql_config)
// const conn = mysql.createConnection(models.handleDisconnection)
// models.handleDisconnection()
// const conn = models.handleDisconnection

// conn.connect()
const jsonWrite = function (res, ret) {
    if (typeof ret === 'undefined') {
        res.json({
            code: '1',
            msg: '操作失败'
        })
    } else {
        res.json({
            "code": '200',
            "msg": 'ok'
        })
    }
}
const jsonWriteShow = function (res, ret) {
    if (typeof ret === 'undefined') {
        res.json({
            code: '1',
            msg: '操作失败'
        })
    } else {
        res.json({
                code: '200',
                msg: 'ok',
                data:{
                    a:10,
                    b:20,
                    c:[1,2,34],
                    ret
                },
                
            }

        )
    }
}
// 接口：增加信息
router.post('/addDetail', (req, res) => {
    const sql = $sql.Detail.add
    const params = req.body
    const time = new Date()
    const url=params.url?params.url:''
    const weburl=params.weburl?params.weburl:''
    const cs_id=0
    const category=params.category?params.category:''
    console.log('添加', params)
    conn.query(sql, [params.title, time, params.src, category, params.pic, url, weburl, params.content, cs_id], function (err, result) {
        if (err) {
            console.log(err)
        }
        if (result) {
            jsonWrite(res, result)
        }
        
    })
    conn.close;
})
router.post('/addDetails', (req, res) => {
    const sql = $sql.Detail.add
    const params = req.body
    const time = new Date()
    for (let i = 0; i < params.length; i++) {
        console.log(params[i].title)
        conn.query(sql, [params[i].title, time, params[i].src, params[i].category, params[i].pic, params[i].url, params[i].weburl, params[i].content, params[i].cs_id], function (err, result) {
            if (err) {
                console.log(err)
            }
            if (result) {
                jsonWrite(res, result)
            }
        })
        conn.close;
    }
    // console.log('添加', params)
    
})

// 接口：查詢信息
router.get('/showDetail', (req, res) => {
    const sql = $sql.Detail.show
    const sqlAll = $sql.Detail.showAll
    const params = req.query
    console.log('查询', params)
    if (params.cs_id) {
        conn.query(sql, [params.cs_id], function (err, result) {
            if (err) {
                console.log(err)
            }
            if (result) {
                jsonWriteShow(res, result)
            }
        })
        conn.close;
    } else {
        conn.query(sqlAll, function (err, result) {
            if (err) {
                console.log(err)
            }
            if (result) {
                jsonWriteShow(res, result)
            }
        })
        conn.close;
    }
})
router.get('/showlist', (req, res) => {
    const sql = $sql.Detail.showlist
    const params = req.query
    console.log('查询', params)
   
        conn.query(sql, [params.id], function (err, result) {
            if (err) {
                console.log(err)
            }
            if (result) {
                jsonWriteShow(res, result)
            }
        })
        conn.close;
})

// 接口：删除信息
router.delete('/deleteDetail', (req, res) => {
    const sql = $sql.Detail.del
    const params = req.query
    console.log('删除', params)
    conn.query(sql, [params.id], function (err, result) {
        if (err) {
            console.log(err)
        }
        if (result) {
            jsonWrite(res, result)
        }
    })
    conn.close;
})

// 更新信息
router.post('/updateDetail', (req, res) => {
    const sql = $sql.Detail.update
    const params = req.body
    const time = new Date()
    const url=params.url?params.url:''
    const weburl=params.weburl?params.weburl:''
    const cs_id=0
    const category=params.category?params.category:''
    console.log('更新')
    conn.query(sql, [params.title, time, params.src, category, params.pic, url, weburl, params.content, cs_id, params.id], function (err, result) {
        if (err) {
            console.log(err)
        }
        if (result) {
            jsonWrite(res, result)
        }
    })
    conn.close;
})

//搜索接口
router.get('/showsearch', (req, res)=>{
    const sql = $sql.Detail.showsearch
    const params = req.query
    console.log('搜索',params)
    conn.query(sql, [ '%'+ params.title +'%'], function (err, result) {
        if (err) {
            console.log(err)
        }
        if (result) {
            jsonWriteShow(res, result)
        }
    })
    conn.close;
})


module.exports = router